package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.ButcherAdmissionInspectionStatus;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.ButcherAdmissionInspectionDao;
import com.jiantong.work.model.ButcherAdmissionInspection;
import com.jiantong.work.service.ButcherAdmissionInspectionReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by niufei on 2017/3/17.
 * 入场监督检查
 */
@Slf4j
@Service
@RpcProvider
public class ButcherAdmissionInspectionReadServiceImpl implements ButcherAdmissionInspectionReadService{

    private final ButcherAdmissionInspectionDao butcherAdmissionInspectionDao;

    @Autowired
    public ButcherAdmissionInspectionReadServiceImpl(ButcherAdmissionInspectionDao butcherAdmissionInspectionDao) {
        this.butcherAdmissionInspectionDao = butcherAdmissionInspectionDao;
    }

    @Override
    public Response<ButcherAdmissionInspection> findById(Long id) {
        try{
            ButcherAdmissionInspection butcherAdmissionInspection = butcherAdmissionInspectionDao.findById(id);
            if(butcherAdmissionInspection == null){
                log.error("butcherAdmissionInspection(id={}) is not found", id);
                return Response.fail("butcherAdmissionInspection.not.found");
            }
            return Response.ok(butcherAdmissionInspection);
        }catch (Exception e){
            log.error("failed to find butcherAdmissionInspection(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("butcherAdmissionInspection.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherAdmissionInspection>> pagingButcherAdmissionInspection(Integer pageNo, Integer pageSize, Integer butcherSelected, LoginUser loginUser) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            if(butcherSelected != null){
                criteria.put("butcherSelected",butcherSelected);
            }
            List<Long> butcherFactoryIds = null;
            Integer organizationType = loginUser.getOrganizationType();
            if(organizationType == 3){
                butcherFactoryIds = loginUser.getButcherFactoryIds();
                if(butcherFactoryIds != null && butcherFactoryIds.size() != 0){
                    criteria.put("butcherFactoryIds",butcherFactoryIds);
                }else {
                    return Response.ok(new Paging<>(0L,new ArrayList<>()));
                }
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(butcherAdmissionInspectionDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherAdmissionInspection(status={}), cause:{}",butcherSelected,Throwables.getStackTraceAsString(e));
            return Response.fail("ButcherAdmissionInspection.paging.fail");
        }
    }

    @Override
    public Response<Paging<ButcherAdmissionInspection>> pagingButcherAdmissionInspectionByButcherFactoryId(Integer pageNo, Integer pageSize,LoginUser loginUser) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Integer butcherSelected = -1;
            Integer status = 1;
            Map<String , Object> criteria = Maps.newHashMap();
            Long butcherFactoryId = loginUser.getOrganizationId();
            if(butcherFactoryId != null){
                criteria.put("butcherFactoryId",butcherFactoryId);
            }
            criteria.put("butcherSelected",butcherSelected);
            criteria.put("status",status);
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(butcherAdmissionInspectionDao.pagingByButcherFactoryId(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherAdmissionInspection(), cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("ButcherAdmissionInspection.paging.fail");
        }
    }

    @Override
    public Response<Paging<ButcherAdmissionInspection>> pagingButcherAdmissionInspectionAll(Integer pageNo, Integer pageSize, Integer butcherSelected) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            if(butcherSelected != null){
                criteria.put("butcherSelected",butcherSelected);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(butcherAdmissionInspectionDao.pagingButcherAdmissionInspection(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherAdmissionInspection(status={}), cause:{}",butcherSelected,Throwables.getStackTraceAsString(e));
            return Response.fail("ButcherAdmissionInspection.paging.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = butcherAdmissionInspectionDao.findStatusByID(id);
            Map<String,Object> map = Maps.newHashMap();
            map.put("status",status);

            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find status(id={}), cause:{}",id,Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find status");
        }
    }

    @Override
    public Response<List<Map<String, BigDecimal>>> findSum(Date start, Date end, Integer productType) {
        try{
            Map<String,Object> map = Maps.newHashMap();
            map.put("status", ButcherAdmissionInspectionStatus.EFFECTIVE.value());
            if(start != null){
                map.put("start",start);
            }
            if(end != null){
                map.put("end",end);
            }
            if(productType != null){
                map.put("productType",productType);
            }
            List<Map<String , BigDecimal>> list = butcherAdmissionInspectionDao.findSum(map);
            if(list == null){
                log.error("Sum normal(productType={}) is not found", productType);
                return Response.fail("Sum.normal.not.found");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find Sum normal (productType={}), cause:{}", productType, Throwables.getStackTraceAsString(e));
            return Response.fail("Sum.normal.find.fail");
        }
    }

    @Override
    public Response<List<Map<String, Integer>>> findSumIsLocal(Date start, Date end, Integer productType) {
        try{
            Map<String,Object> map = Maps.newHashMap();
            map.put("status", ButcherAdmissionInspectionStatus.EFFECTIVE.value());
            if(start != null){
                map.put("start",start);
            }
            if(end != null){
                map.put("end",end);
            }
            if(productType != null){
                map.put("productType",productType);
            }
            List<Map<String , Integer>> list = butcherAdmissionInspectionDao.findSumIsLocal(map);
            if(list == null){
                log.error("Sum IsLocal(productType={}) is not found", productType);
                return Response.fail("Sum.IsLocal.not.found");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find Sum IsLocal (productType={}), cause:{}", productType, Throwables.getStackTraceAsString(e));
            return Response.fail("Sum.IsLocal.find.fail");
        }
    }

    @Override
    public Response<List<Map<String, BigDecimal>>> findSumButcherFactory(Date start, Date end, Integer productType) {
        try{
            Map<String,Object> map = Maps.newHashMap();
            map.put("status", ButcherAdmissionInspectionStatus.EFFECTIVE.value());
            map.put("butcherSelected",1);
            if(start != null){
                map.put("start",start);
            }
            if(end != null){
                map.put("end",end);
            }
            if(productType != null){
                map.put("productType",productType);
            }
            List<Map<String , BigDecimal>> list = butcherAdmissionInspectionDao.findSumButcherFactory(map);
            if(list == null){
                log.error("Sum ButcherFactory(productType={}) is not found", productType);
                return Response.fail("Sum.ButcherFactory.not.found");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find Sum ButcherFactory (productType={}), cause:{}", productType, Throwables.getStackTraceAsString(e));
            return Response.fail("Sum.ButcherFactory.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherAdmissionInspection>> pagingcommon(Integer pageNo, Integer pageSize, Map criteria) {
        if(criteria==null){
            criteria = new HashMap();
        }
        PageInfo pageInfo = new PageInfo(pageNo,pageSize);
        return Response.ok(butcherAdmissionInspectionDao.pagingCommon(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
    }
}
